<?php
class Controller_Base extends FLEA_Controller_Action {
    function Controller_Base() {
        if (isset($_SESSION['LANG'])) {
            $lang = $_SESSION['LANG'];
            $languages = FLEA::getAppInf('languages');
            if (in_array($lang, $languages, true)) {
                FLEA::setAppInf('defaultLanguage', $lang);
            }
        }
        load_language('ui, exception');
    }
    function _prepareData(& $meta) {
        $data = array();
        foreach ($meta as $m) {
            if (isset($_POST[$m['name']])) {
                $data[$m['name']] = $_POST[$m['name']];
            } else {
                if (isset($m['defaultValue'])) {
                    $data[$m['name']] = $m['defaultValue'];
                } else {
                    $data[$m['name']] = null;
                }
            }
        }
        return $data;
    }
    function _goBack() {
        $url = $this->_getBack();
        unset($_SESSION['BACKURL']);
        redirect($url);
    }
    function _setBack() {
        $_SESSION['BACKURL'] = encode_url_args($_GET);
    }
    function _getBack() {
        if (isset($_SESSION['BACKURL'])) {
            $url = $this->rawurl($_SESSION['BACKURL']);
        } else {
            $url = $this->_url();
        }
        return $url;
    }
    function rawurl($queryString) {
    	if (substr($queryString, 0, 1) == '?') {
    		$queryString = substr($queryString, 1);
    	}
    	return $_SERVER['SCRIPT_NAME'] . '?' . $queryString;
    }
}
?>